package net.yla.smartbus.core.util;

import org.apache.log4j.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * 日期工具
 *
 * @author wangpan
 * @category
 * @see
 */
@Deprecated
public class DateUtils {
    private static final Logger logger = Logger.getLogger(DateUtils.class);

    final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:hh:ss";

    public static String formatDateDefault(Date date, String pattern) {
        if (date == null) {
            return "";
        }

        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            return formatter.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 根据给定的格式化参数，将字符串转换为日期
     *
     * @param dateString
     * @param dateFormat
     * @return java.util.Date
     */
    public static Date parse(String dateString, String dateFormat) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>parse(String dateString, String dateFormat)");
        }
        if (dateString == null || "".equals(dateString.trim()) ) {
            return null;
        }
        DateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = null;
        try {
            date = sdf.parse(dateString);

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (logger.isDebugEnabled()) {
            logger.debug("<<<parse(return java.util.Date)");
        }
        return date;
    }

    /**
     * 默认将字符串转换为日期，格式(yyyy-MM-dd)
     *
     * @param dateString
     * @return
     */
    public static Date parse(String dateString) {
        return parse(dateString, DEFAULT_FORMAT);
    }

    /**
     * 根据给定的格式化参数，将日期转换为字符串
     *
     * @param date
     * @param dateFormat
     * @return String
     */
    public static String toString(Date date, String dateFormat) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>> toString(java.util.Date date, String dateFormat)");
        }
        if (date == null||"".equals(date) ) {
            return "bug: date is null";
        }
        DateFormat sdf = new SimpleDateFormat(dateFormat);
        String str = sdf.format(date);

        if (logger.isDebugEnabled()) {
            logger.debug("<<< toString return String");
        }
        return str;
    }

    /**
     * 默认将日期转换为字符串，格式(yyyy-MM-dd)
     *
     * @param date
     * @return String
     */
    public static String toString(Date date) {
        return toString(date, DEFAULT_FORMAT);
    }

    /**
     * 将日期转换为长整型?
     *
     * @param date
     * @return long
     */
    public static long toLong(Date date) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>toLong(java.util.Date date)");
        }
        if (date == null) {
            return 0;
        }
        long d = date.getTime();

        if (logger.isDebugEnabled()) {
            logger.debug("<<<toLong return long");
        }
        return d;
    }

    /**
     * 将长整型转换为日期对象
     *
     * @param time
     * @return date
     */
    public static Date toDate(long time) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>toDate (long time)");
        }
//        if ("".equals(time)) {
//            return new Date();
//        }
        Date date = new Date(time);

        if (logger.isDebugEnabled()) {
            logger.debug("<<<toDate return date");
        }
        return date;
    }

    /**
     * 获得系统当前时间
     *
     * @return java.util.Date
     */
    public static String currentStringDate() {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>currentDate()");
        }
        Date date = new Date();

        if (logger.isDebugEnabled()) {
            logger.debug("<<<currentDate() return date");
        }
        return toString(date);
    }

    /**
     * 获得系统当前时间(按用户自己格式)
     *
     * @return java.util.Date
     */
    public static String currentYourDate(String formate) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>currentDate()");
        }
        Date date = new Date();

        if (logger.isDebugEnabled()) {
            logger.debug("<<<currentDate() return date");
        }
        return toString(date, formate);
    }


    /**
     * 获得系统当前时间
     *
     * @return java.util.Date
     */
    public static Date currentDate() {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>currentDate()");
        }
        Date date = new Date();

        if (logger.isDebugEnabled()) {
            logger.debug("<<<currentDate() return date");
        }
        return date;
    }

    /**
     * 根据日历的规则，为给定的日历字段添加或减去指定的时间�?
     *
     * @param field 指定的日历字段
     * @param date  需要操作的日期对象
     * @param value 更改的时间值
     * @return java.util.Date
     */
    public static Date add(int field, Date date, int value) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>add(int field,Date date,int value)");
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(field, value);
        Date newDate = ca.getTime();

        if (logger.isDebugEnabled()) {
            logger.debug("<<<add() return date");
        }
        return newDate;
    }

    /**
     * 返回给定日历字段的值
     *
     * @param field 指定的日历字段
     * @param date  给定的日期对象
     * @return java.util.Date
     */
    public static int get(int field, Date date) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>get(int field, Date date");
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int value = ca.get(field);

        if (logger.isDebugEnabled()) {
            logger.debug("<<<get() return date");
        }
        return value;
    }

    /**
     * 返回前N个月的日期值
     *
     * @param month
     * @return
     */
    public static Date getLastMonth(String month) {
        Calendar ca = Calendar.getInstance();
        int m = 0;
        try {
            m = Integer.parseInt(month);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        ca.add(Calendar.MONTH, -m);
        return ca.getTime();
    }


    /**
     * 验证字符串是否匹配日期
     */
    public static boolean isDate(String str, String formate) {
        try {
            DateFormat sdf = new SimpleDateFormat(formate);
            sdf.parse(str);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return false;
        }
    }

    /**
     * 获取偏移日期
     *
     * @param offset
     * @return
     */
    public static Date getOffsetDate(int offset) {

        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + offset);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDate;
    }

    /**
     * 字符串日期格式的计算
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days)) + 1;
    }

    /**
     * 字符串日期格式的计算
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String start = sdf.format(smdate);
        String end = sdf.format(bdate);
        cal.setTime(sdf.parse(start));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(end));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days)) + 1;
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDate(Date start, Date end) {
        //日期集合
        List<String> list = getDate(start, end, "yyyy-MM-dd");
        return list;
    }

    /**
     * 将秒转化为 *天*时*分*秒
     *
     * @param second
     * @return
     */
    public static String secondToTime(long second) {
        long days = second / 86400;            //转换天数
        second = second % 86400;            //剩余秒数
        long hours = second / 3600;          //转换小时
        second = second % 3600;              //剩余秒数
        long minutes = second / 60;          //转换分钟
        second = second % 60;                //剩余秒数
        if (days > 0) {
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        } else if (hours > 0) {
            return hours + "小时" + minutes + "分" + second + "秒";
        } else if (minutes > 0) {
            return minutes + "分" + second + "秒";
        } else {
            return second + "秒";
        }
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param start
     * @param end
     * @param pattern 日期格式 如 yyyy.MM.dd
     * @return
     */
    public static List<String> getDate(Date start, Date end, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        //保存日期集合
        List<String> list = new ArrayList<>();
        try {
            //用Calendar 进行日期比较判断
            Calendar cd = Calendar.getInstance();
            while (start.getTime() <= end.getTime()) {
                list.add(sdf.format(start));
                cd.setTime(start);
                //增加一天 放入集合
                cd.add(Calendar.DATE, 1);
                start = cd.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获得指定天数前的指定时间
     * day是指当前天数的前或后几天  今天是0 昨天是-1
     */
    public static String getDesignedDate(int day, int hour, int min, int second) {
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, min);
        calendar.set(Calendar.SECOND, second);
        Date startDate = calendar.getTime();//开始时间 yyyy:MM:dd 00:00:00

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(startDate);
    }

    /**
     * 给日期拼接 时间
     *
     * @param date 待拼接的日期
     * @param time 拼接的时间 如22:30:00
     * @return java.util.Date
     */
    public static Date parse(Date date, String time) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>parse(Date date, String time)");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String start = sdf.format(date);
        if (time == null) {
            time = " 23:59:59";
        }
        String addTime = start + time;
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(addTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 时间加分钟计算
     *
     * @param strDate
     * @param amount
     * @return
     */
    public static Date dateHold(String strDate, String amount) {
        String[] tmp1 = strDate.split("\\.");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(tmp1[0]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        Calendar after = (Calendar) calendar.clone();
        if (Double.valueOf(amount) < 1) {
            after.add(Calendar.SECOND, (int) (Double.valueOf(amount) * 60));
        } else {
            after.add(Calendar.MINUTE, Integer.valueOf(amount));
        }
        return after.getTime();
    }

    /**
     * 字符串转换成date
     *
     * @param date
     * @return
     */
    public static Date stringToDate(String date,String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static long valueOfTime(Date date,Date dates){
        long diff = date.getTime() - dates.getTime();// 这样得到的差值是微秒级别
        long days = diff / (1000 * 60 * 60 * 24);//天
        long hours = (diff - days * (1000 * 60 * 60 * 24))
                / (1000 * 60 * 60);    //小时
        long sc = (diff - days * (1000 * 60 * 60 * 24) - hours
                * (1000 * 60 * 60)) / (1000); // 秒
        return sc;
    }

    /**
     * Date 取消时分秒
     * @param date
     * @return
     */
    public Date changeDate(Date date){
        LocalDate localDate=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Date newDate=java.sql.Date.valueOf(localDate);
        return newDate;
    }
}

